<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ProjectRecord_model extends MY_Model{
	public function __construct()
    {
        parent::__construct();
    }
	public function pm_project_one($id,$field='*'){
		$this->db->where('id',$id);
		$this->db->select($field);
		$query=$this->db->get('pm_project');
		return $query->row_array();
	}
    //项目进度列表
    public function pm_projectrecord_ajax_list($where=array()){
        //总数
        $this->db->select('count(id) as counts');
        $this->com_where($where);
        $query=$this->db->get('pm_project_record');
        $counts=$query->row_array();
            //多条
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$this->pagesize * ($page - 1);
        $this->com_where($where);
        $this->db->limit($this->pagesize,$startRow);
        $this->db->order_by('id desc');
        $query=$this->db->get('pm_project_record');
        $data['list']=$query->result_array();
        $data['pageshow']=$this->pageajax_show($this->pagesize,$counts['counts']);
        return $data;
    }
	//研发人员项目进度列表
    public function pm_projectrecord_ajax_list_yf($where=array()){
		$this->com_where($where);
		$this->db->select('count(A.id) as counts');
		$this->db->from('pm_project_record as A');
		$this->db->join('pm_project_record_developing as B', 'A.id = B.recordid');
		$query = $this->db->get();
		$counts=$query->row_array();
		     //多条
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$this->pagesize * ($page - 1);
        $this->com_where($where);
		$this->db->select('A.*,B.yfid');
		$this->db->from('pm_project_record as A');
		$this->db->join('pm_project_record_developing as B', 'A.id = B.recordid');
        $this->db->limit($this->pagesize,$startRow);
        $this->db->order_by('id desc');
        $query=$this->db->get();
        $data['list']=$query->result_array();
        $data['pageshow']=$this->pageajax_show($this->pagesize,$counts['counts']);
        return $data;
    }
    //获取项目状态单条信息
    public function getPmProjectRecordOne($id,$field='*'){
        $this->db->where('id',$id);
		$this->db->select($field);
        $query=$this->db->get('pm_project_record');
        return $query->row_array();
    }
    //添加项目进度信息
    public function pm_project_record_add($data){
        $pmsuserinfo=unserialize($_SESSION['pmsuserinfo']);
        $attachment=$data['attachment'];
        unset($data['attachment']);
        $data['adduid']=$pmsuserinfo['id'];
        $data['addtime']=time();
        $this->db->trans_begin();
        $this->db->insert('pm_project_record',$data);
        $addid=$this->db->insert_id('id');
        $attachmentdata=array();
        if($attachment!=''){
            $attachmentarr=explode(',',$attachment);
            foreach($attachmentarr as $k=>$v){
                 $attachmentdata[$k]['recordid']=$addid;
                 $attachmentdata[$k]['filepath']=$v;
                 $attachmentdata[$k]['uptime']=time();
                 $attachmentdata[$k]['upuid']=$pmsuserinfo['id'];
            }
        }
        if(!empty($attachmentdata)){
            $this->db->insert_batch('pm_project_files',$attachmentdata);
        }
        if($this->db->trans_status()===false){
            $this->db->trans_rollback();
            return false;
        }else{
            $this->db->trans_commit();
            return $addid;
        }
    }
    //编辑项目状态
    public function pm_project_record_edit($data){
        $pmsuserinfo=unserialize($_SESSION['pmsuserinfo']);
        $attachment=$data['attachment'];
        unset($data['attachment']);
        if($attachment!=''){
            $attachmentdata=explode(',',$attachment);
        }else{
            $attachmentdata=array();
        }
        //查询原来是否存在附件
        $this->db->where('recordid',$data['id']);
        $query=$this->db->get('pm_project_files');
        $attachmentlist=$query->result_array();
        $addfiles=array();//新上传附件
        $delfiles=array();//删除原不要附件
        if(empty($attachmentlist)){//原来没有附件
            $addfiles=$attachmentdata;
        }else{//原存在附件
            $oldattachmentarr=array();
            foreach($attachmentlist as $k=>$v){
                $oldattachmentarr[]=$v['filepath'];
            }
            $addfiles=array_diff($attachmentdata,$oldattachmentarr);
            $delfiles=array_diff($oldattachmentarr,$attachmentdata);
        }
        //事物
        $this->db->trans_begin();
        $this->db->where('id',$data['id']);
        $this->db->update('pm_project_record',$data);
        if(!empty($addfiles)){
            $addfiles_arr=array();
            foreach ($addfiles as $k => $v) {
                $addfiles_arr[$k]['recordid']=$data['id'];
                $addfiles_arr[$k]['filepath']=$v;
                $addfiles_arr[$k]['uptime']=time();
                $addfiles_arr[$k]['upuid']=$pmsuserinfo['id'];
            }
            $this->db->insert_batch('pm_project_files',$addfiles_arr);
        }
        if(!empty($delfiles)){
            $this->db->where_in('filepath',$delfiles);
            $this->db->delete('pm_project_files');
        }
        if($this->db->trans_status()===false){
            $this->db->trans_rollback();
            return false;
        }else{
            $this->db->trans_commit();
            return true;
        }

    }
    //编辑审核状态
    public function pm_project_record_edit_verify($id,$verify){
        $data['status']=$verify;
        $this->db->where('id',$id);
        $this->db->update('pm_project_record',$data);
        if($this->db->affected_rows()>0){
            return true;
        }else{
            return false;
        }
    }
    //获取项目信息
    public function getProjectList($proid,$field='*'){
        $this->db->where('id',$proid);
        $query=$this->db->get('pm_project');
        return $query->row_array();
    }
    //获取所有项目
    public function getProjectAllList($field='*'){
        $this->db->select($field);
        $query=$this->db->get('pm_project');
        return $query->result_array();
    }
    //获取所有客户
    public function getCustmoerAllList($field='*'){
        $this->db->select($field);
        $query=$this->db->get('pm_custmoer');
        return $query->result_array();
    }
	//获取客户
    public function getCustmoerInfo($custmoerarr,$field='*'){
        $this->db->where_in('id',$custmoerarr);
        $this->db->select($field);
        $query=$this->db->get('pm_custmoer');
        return $query->result_array();
    }
    //获取用户信息
    public function getUserInfo($useridarr,$field='*'){
        $this->db->where_in('id',$useridarr);
        $this->db->select($field);
        $query=$this->db->get('pm_user');
        return $query->result_array();
    }
    //获取附件信息
    public function getattachmentlist($recordid,$field='*'){
        $this->db->select($field);
        $this->db->where('recordid',$recordid);
        $query=$this->db->get('pm_project_files');
        return $query->result_array();
    }
    public function writeProjectChange($data){
        $pmsuserinfo=unserialize($_SESSION['pmsuserinfo']);
        $arr['adduid']=$pmsuserinfo['id'];
        $arr['addtime']=time();
        $arr['recordid']=$data['id'];
        $arr['customer_id']=$data['customer_id'];
        $arr['type']=$data['type'];
        $arr['status']=$data['status'];
        $arr['remark']=$data['remark'];
        $this->db->insert('pm_project_change',$arr);
        return true;
    }
	//项目记录日志操作
    public function pm_projectrecord_log_list($id){
        $this->db->where('recordid',$id);
        $this->db->order_by('id desc');
        $query=$this->db->get('pm_project_change');
        return $query->result_array();
    }
	//查询研发人员
	public function getYanFaUserList(){
		$this->db->where('type',0);
		$this->db->select('id,name,displayname');
		$query=$this->db->get('pm_user');
		return $query->result_array();
	}
	//研发审核数据写入
	public function writePmProjectRecordDeveloping($recordid,$yfarr){
		$pmsuserinfo=unserialize($_SESSION['pmsuserinfo']);
		if(!empty($yfarr)){
			$arr=array();
			foreach($yfarr as $k=>$v){
				$arr[$k]['recordid']=$recordid;
				$arr[$k]['yfid']=$v;
				$arr[$k]['adduid']=$pmsuserinfo['id'];
				$arr[$k]['addtime']=time();
			}
			$this->db->insert_batch('pm_project_record_developing',$arr);
			return $this->db->affected_rows();
		}else{
			return false;
		}
	}
	//获取研发名单信息
	public function getPmProjectRecordDeveloping($recordid,$field='*'){
		$this->db->where('recordid',$recordid);
		$this->db->select($field);
		$query=$this->db->get('pm_project_record_developing');
		return $query->result_array();
	}
	//删除项目任务 必须是新建模式
	public function delPmProjectRecord($id){
		$this->db->trans_begin();
		//删除附件
		$this->db->where('recordid',$id);
		$this->db->delete('pm_project_files');
		
		$this->db->where('recordid',$id);
		$this->db->delete('pm_project_change');
		
		$this->db->where('id',$id);
		$this->db->delete('pm_project_record');
		if($this->db->trans_status()===false){
			$this->db->trans_rollback();
			return false;
		}else{
			$this->db->trans_commit();
			return true;
		}
	}
}
?>